package all.com.jinyongqunxiax.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;

import all.com.jinyongqunxiax.JxEditorApplication;

public class JxHud {
    public static void alert(String fmt, Object... args) {
        String message = String.format(fmt, args);
        alert(message);
    }

    public static void alert(String message) {
        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            Context context = JxEditorApplication.shared.getApplicationContext();
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Context context = JxEditorApplication.shared.getApplicationContext();
                    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    public static void info(String fmt, Object... args) {
        String message = String.format(fmt, args);
        info(message);
    }
    public static void info(String message) {
        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            Context context = JxEditorApplication.shared.getApplicationContext();
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Context context = JxEditorApplication.shared.getApplicationContext();
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public static Object loading(Context context, String message) {
        return KProgressHUD.create(context).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                           // .setLabel("Please wait")
                           // .setDetailsLabel(message)
                           .setCancellable(true)
                           .setAnimationSpeed(2)
                           .setDimAmount(0.5f)
                           .show();
    }

    public static void hideLoading(Object hud) {
        if (hud instanceof KProgressHUD) {
            KProgressHUD khud = (KProgressHUD) hud;
            khud.dismiss();
        }
    }
}
